//
//  MapPointViewController.m
//  Runone
//
//  Created by runone01 on 16/9/1.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "MapPointViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "Define.h"

@interface MapPointViewController ()<MAMapViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong) MAMapView *mapView;//地图
@property (nonatomic, strong)MAPointAnnotation *pointAnnotation;
@end

@implementation MapPointViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadMapView];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"确 定" style:UIBarButtonItemStylePlain target:self action:@selector(returnVC)];
    
   // [self defaultAnnimation];

    // Do any additional setup after loading the view.
}

-(void)viewDidAppear:(BOOL)animated
{
    
}
-(void)loadMapView
{
    [AMapServices sharedServices].apiKey = GETGAODEPAIKEY;
    
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    _mapView.delegate = self;
    
    _mapView.showsCompass = NO;
    _mapView.showsScale= NO;
    
    _mapView.showsUserLocation=YES;
    [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES];
    [self.view addSubview:_mapView];
    
    //长按屏幕，添加大头针
    UITapGestureRecognizer *Lpress=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressClick:)];
    Lpress.delegate = self;
    [self.mapView addGestureRecognizer:Lpress];
    
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

-(void)creatAnnotion:(CLLocationCoordinate2D)touchPoint
{
    _pointAnnotation = [[MAPointAnnotation alloc] init];
    _pointAnnotation.coordinate=touchPoint;
//    _mapView.centerCoordinate=touchPoint;
    [_mapView addAnnotation:_pointAnnotation];
    
}

//长按事件响应
-(void)LongPressClick:(UITapGestureRecognizer *)gestureRecognizer
{
    [self.mapView removeAnnotation:_pointAnnotation];
//    if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
//    {
//        return;
//    }
    
    //坐标转换
    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
    CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
    
//    if (self.FloatBlock!=nil) {
//        self.FloatBlock([NSString stringWithFormat:@"%f",touchMapCoordinate.latitude],[NSString stringWithFormat:@"%f",touchMapCoordinate.longitude]);
//    };
    CGFloat la=touchMapCoordinate.latitude;
    CGFloat lo=touchMapCoordinate.longitude;
    if (self.FloatBlock) {
        self.FloatBlock(la,lo);
    };
    NSLog(@"%f,%f",la,lo);
    NSLog(@"%f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);
    [self creatAnnotion:touchMapCoordinate];
    
}

-(void)returnVC
{
   
    [self.navigationController popViewControllerAnimated:YES];
}

//-(void)defaultAnnimation
//{
//    
//    MACoordinateSpan span = MACoordinateSpanMake(0.8, 0.9);
//    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(25.1902561, 113.212982);
//    MACoordinateRegion region = MACoordinateRegionMake(centerCoordinate, span);
//    _mapView.region = region;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
